package mil.af.amc.adpetracker.dao.hibernate;

import mil.af.amc.adpetracker.dao.HandReceiptDao;
import mil.af.amc.adpetracker.model.AdpeAccount;
import mil.af.amc.adpetracker.model.HandReceipt;
import mil.af.amc.adpetracker.model.User;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.util.List;


/**
 * Hibernate implementation of the HandReceiptDao interface
 *
 * @author TSgt Ferguson
 */
@SuppressWarnings({"unchecked"})
@Repository("handReceiptDao")
public class HibernateHandReceiptDao extends HibernateGenericDao<HandReceipt> implements HandReceiptDao {

    public HibernateHandReceiptDao() {
        super(HandReceipt.class);
    }

    public List<HandReceipt> getByAdpeAccount(AdpeAccount acct) {

        Criteria hrCriteria = sessionFactory.getCurrentSession().createCriteria(HandReceipt.class);

        //DetachedCriteria crit = DetachedCriteria.forClass(HandReceipt.class);
        hrCriteria.add(Restrictions.eq("adpeAccount", acct));
        return hrCriteria.list();
        //return (List<HandReceipt>) getHibernateTemplate().findByCriteria(crit);
    }

    public List<HandReceipt> getByUser(User user) {

        Criteria hrCriteria = sessionFactory.getCurrentSession().createCriteria(HandReceipt.class);

        //DetachedCriteria crit = DetachedCriteria.forClass(HandReceipt.class);
        hrCriteria.add(Restrictions.eq("user", user));

        return hrCriteria.list();

        //return (List<HandReceipt>) getHibernateTemplate().findByCriteria(crit);
    }

    public HandReceipt getByName(String name) {

        Criteria hrCriteria = sessionFactory.getCurrentSession().createCriteria(HandReceipt.class);

        //DetachedCriteria crit = DetachedCriteria.forClass(HandReceipt.class);
        hrCriteria.add(Restrictions.ilike("name", name));

        HandReceipt results = (HandReceipt) hrCriteria.uniqueResult();
        return results;
//        if (results.size()==0) {
//            return null;
//        }
//
//        return results.get(0);
    }
}
